/*
    Pureport, a report generator for Java
    Copyright (C) 2011  Maurice Perry <maurice@perry.ch>

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/
package org.librebiz.pureport.ui;

import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Rectangle;
import java.awt.Toolkit;
import java.awt.geom.Rectangle2D;
import java.awt.print.PageFormat;
import java.awt.print.Printable;
import java.awt.print.PrinterException;
import java.awt.print.PrinterJob;
import javax.swing.JDialog;
import javax.swing.JFrame;

import javax.swing.JOptionPane;
import org.librebiz.pureport.definition.PageLayout;
import org.librebiz.pureport.quantity.Unit;

import org.librebiz.pureport.run.Formatter;
import org.librebiz.pureport.run.Page;
import org.librebiz.pureport.run.PageStore;


public class PreviewDialog extends JDialog {
    private Formatter formatter;
    private PageStore pageStore;
    private PrinterJob job;

    public PreviewDialog(JFrame parent, Formatter formatter,
            PageStore pageStore) {
        super(parent, true);
        initComponents();
        this.formatter = formatter;
        this.pageStore = pageStore;
        pagePreview.setPageLayout(formatter.getPageLayout());
        job = PrinterJob.getPrinterJob();
        formatter.format(job.defaultPage());
        fillPageCombo();
        Rectangle rect;
        if (parent != null) {
            rect = parent.getBounds();
        } else {
            Toolkit tk = Toolkit.getDefaultToolkit();
            Dimension dim = tk.getScreenSize();
            rect = new Rectangle(0,0,dim.width,dim.height);
        }
        setLocation(rect.x+(rect.width-getWidth())/2,
                rect.y+(rect.height-getHeight())/2);
        getRootPane().setDefaultButton(print);
    }

    private void fillPageCombo() {
        try {
            pageCombo.removeAllItems();
            for (int i = 1; i <= pageStore.getPageCount(); ++i) {
                pageCombo.addItem(i);
            }
        } finally {
        }
        if (pageCombo.getItemCount() > 0) {
            pageCombo.setSelectedIndex(0);
        }
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc=" Generated Code ">//GEN-BEGIN:initComponents
    private void initComponents() {
        pagePreview = new org.librebiz.pureport.components.PagePreview();
        topPanel = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        pageCombo = new javax.swing.JComboBox();
        print = new javax.swing.JButton();
        setup = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle(java.util.ResourceBundle.getBundle("org/librebiz/pureport/ui/preview").getString("preview"));
        pagePreview.setMinimumSize(new java.awt.Dimension(100, 150));
        pagePreview.setPreferredSize(new java.awt.Dimension(300, 400));
        getContentPane().add(pagePreview, java.awt.BorderLayout.CENTER);

        jLabel1.setText(java.util.ResourceBundle.getBundle("org/librebiz/pureport/ui/preview").getString("page"));
        topPanel.add(jLabel1);

        pageCombo.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                pageComboActionPerformed(evt);
            }
        });

        topPanel.add(pageCombo);

        print.setMnemonic('p');
        print.setText(java.util.ResourceBundle.getBundle("org/librebiz/pureport/ui/preview").getString("print"));
        print.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                printActionPerformed(evt);
            }
        });

        topPanel.add(print);

        setup.setMnemonic('s');
        setup.setText(java.util.ResourceBundle.getBundle("org/librebiz/pureport/ui/preview").getString("page-setup"));
        setup.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                setupActionPerformed(evt);
            }
        });

        topPanel.add(setup);

        getContentPane().add(topPanel, java.awt.BorderLayout.NORTH);

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void setupActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_setupActionPerformed
        PageFormat pf = job.pageDialog(formatter.getPageFormat());
        if (pf != null) {
            formatter.format(pf);
            fillPageCombo();
        }
    }//GEN-LAST:event_setupActionPerformed

    private void printActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_printActionPerformed
        try {
            job.setPrintable(new Printable() {
                public int print(Graphics g, PageFormat format,
                        int pageIndex) throws PrinterException {
                    if (pageIndex >= pageStore.getPageCount()) {
                        return Printable.NO_SUCH_PAGE;
                    }
                    Page page = pageStore.getPage(pageIndex);
                    page.resolveForwards(formatter.getContext());
                    PageLayout layout = formatter.getPageLayout();
                    Rectangle2D rc = format == null
                            ? layout.getDrawableArea(Unit.PT)
                            : layout.getDrawableArea(format);
                    page.draw((Graphics2D)g, rc.getX(), rc.getY(),
                            rc.getHeight());
                    return Printable.PAGE_EXISTS;
                }
            }, formatter.getPageFormat());
            if (job.printDialog()) {
                job.print();
            }
        } catch (PrinterException e) {
            JOptionPane.showMessageDialog(this, e.getMessage(),
                    "Erreur d'impression", JOptionPane.ERROR_MESSAGE);
        }
    }//GEN-LAST:event_printActionPerformed

    private void pageComboActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_pageComboActionPerformed
        Integer pageNumber = (Integer)pageCombo.getSelectedItem();
        if (pageNumber != null && formatter != null) {
            int index = pageNumber.intValue()-1;
            Page page = pageStore.getPage(index);
            page.resolveForwards(formatter.getContext());
            pagePreview.setPage(page);
            pagePreview.setPageFormat(formatter.getPageFormat());
            pagePreview.repaint();
        }
    }//GEN-LAST:event_pageComboActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JLabel jLabel1;
    private javax.swing.JComboBox pageCombo;
    private org.librebiz.pureport.components.PagePreview pagePreview;
    private javax.swing.JButton print;
    private javax.swing.JButton setup;
    private javax.swing.JPanel topPanel;
    // End of variables declaration//GEN-END:variables
    
}
